﻿using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeaderCCSLaserUI.Models.VisionModels
{
    public class VM_FindNccModel : FunctionBase
    {
        public override bool Run(ref HObject ho_Image, string paramJson, ref HTuple hv_Row, ref HTuple hv_Column, ref HTuple hv_Angle, ref HTuple hv_Score, ref HObject maxRegion, ref HObject ho_SelectedRegion, ref HTuple v1, ref HTuple v2)
        {
            try
            {
                var MShapeModelParam = JsonConvert.DeserializeObject<NccModelParam>(paramJson);
                HOperatorSet.ReadNccModel(MShapeModelParam.FileName, out var hv_ModelID);
                HOperatorSet.SetNccModelParam(hv_ModelID, "timeout", 300);
                HOperatorSet.FindNccModel(ho_Image, hv_ModelID, (new HTuple(MShapeModelParam.AngleStart)).TupleRad(),
                    (new HTuple(MShapeModelParam.AngleEnd - MShapeModelParam.AngleStart)).TupleRad(), MShapeModelParam.MinScore, 1, 0.5, "true", MShapeModelParam.NumLevels, out hv_Row, out hv_Column,
                    out hv_Angle, out hv_Score);
                HOperatorSet.ClearNccModel(hv_ModelID);
                if (hv_Score.Length > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }

        }
    }
}
